/* 考试查询 */
<template> 
    <div>
        <div v-if="indexKey === 1">
            <condition-card @reset="resetSearch" @search="onSearch" label-position="right" label-width="170px">
                <!-- 第一行 -->
                <el-row :gutter="24" style="height: 50px;">
                    <!-- 学院 -->
                    <el-col :span="8">
                        <el-form-item :label="$t('examinInquiry.college')">
                            <el-select v-model="queryForm.condition.college" @change="collegeChange"  clearable filterable style="width:100%">
                                <el-option
                                    v-for="item in options.college"
                                    :key="item.value"
                                    :label="item.label"
                                    :value="item.value">
                                </el-option>
                            </el-select>
                        </el-form-item>
                    </el-col>
                    <!-- 专业 -->
                    <el-col :span="8">
                        <el-form-item :label="$t('examinInquiry.major')">
                            <el-select v-model="queryForm.condition.major" clearable filterable style="width:100%">
                                <el-option
                                    v-for="item in options.major"
                                    :key="item.value"
                                    :label="item.label"
                                    :value="item.value">
                                </el-option>
                            </el-select>
                        </el-form-item>
                    </el-col>
                    <!-- 年级 -->
                    <el-col :span="8">
                        <el-form-item :label="$t('examinInquiry.grade')">
                            <el-date-picker
                                :clearable="false"
                                style="width: 100%"
                                v-model="queryForm.condition.grade"
                                value-format="yyyy"
                                type="year"
                                placeholder="选择年">
                            </el-date-picker>
                        </el-form-item>
                    </el-col>
                </el-row>
                <!-- 第二行 -->
                <el-row :gutter="24" style="height: 50px;">
                    <!-- 培养层次 -->
                    <el-col :span="8">
                        <el-form-item :label="$t('examinInquiry.cultivationLevel')">
                            <el-select v-model="queryForm.condition.trainingLevel" clearable filterable  style="width:100%">
                                <el-option
                                    v-for="item in options.trainingLevel"
                                    :key="item.value"
                                    :label="item.label"
                                    :value="item.value">
                                </el-option>
                            </el-select>
                        </el-form-item>
                    </el-col>
                    <!-- 培养类别 -->
                    <el-col :span="8">
                        <el-form-item :label="$t('examinInquiry.trainingCategory')">
                            <el-select v-model="queryForm.condition.trainingCategory" clearable filterable  style="width:100%">
                                <el-option
                                    v-for="item in options.trainingCategory"
                                    :key="item.value"
                                    :label="item.label"
                                    :value="item.value">
                                </el-option>
                            </el-select>
                        </el-form-item>
                    </el-col>
                    <!-- 学位类型 -->
                    <el-col :span="8">
                        <el-form-item :label="$t('examinInquiry.degreeType')" >
                            <el-select v-model="queryForm.condition.degreeType" clearable filterable  style="width:100%">
                                <el-option
                                    v-for="item in options.degreeType"
                                    :key="item.value"
                                    :label="item.label"
                                    :value="item.value">
                                </el-option>
                            </el-select>
                        </el-form-item>
                    </el-col>
                </el-row>
                <!-- 第三行 -->
                <el-row :gutter="24" style="height: 50px;">
                    <!-- 学习形式 -->
                    <el-col :span="8">
                        <el-form-item :label="$t('examinInquiry.learningForm')">
                            <el-select v-model="queryForm.condition.formLearning" clearable filterable  style="width:100%">
                                <el-option
                                    v-for="item in options.formLearning"
                                    :key="item.value"
                                    :label="item.label"
                                    :value="item.value">
                                </el-option>
                            </el-select>
                        </el-form-item>
                    </el-col>
                    <!-- 导师 -->
                    <el-col :span="8">
                        <el-form-item :label="$t('examinInquiry.tutor')" >
                            <el-input v-model.trim="queryForm.condition.teacher" :placeholder="$t('examinInquiry.pleaseEnter')" clearable style="width:100%"></el-input>
                        </el-form-item>
                    </el-col>
                    <!-- 成绩 -->
                    <el-col :span="8">
                        <el-form-item :label="$t('examinInquiry.achievement')">
                            <el-select v-model="queryForm.condition.score" clearable filterable  style="width:100%">
                                <el-option
                                    v-for="item in options.score"
                                    :key="item.value"
                                    :label="item.label"
                                    :value="item.value">
                                </el-option>
                            </el-select>
                        </el-form-item>
                    </el-col>
                </el-row>
                <!-- 第四行 -->
                <el-row :gutter="24" style="height: 50px;">
                    <!-- 参加次数 -->
                    <el-col :span="8">
                        <el-form-item :label="$t('examinInquiry.attendanceTimes')" >
                            <el-input-number v-model.trim="queryForm.condition.count" :min="0" placeholder="输入数字" style="width:100%"></el-input-number>
                        </el-form-item>
                    </el-col>
                    <!-- 课号 -->
                    <el-col :span="8">
                        <el-form-item :label="$t('examinInquiry.classNumber')" >
                            <el-input v-model.trim="queryForm.condition.courseCode" :placeholder="$t('examinInquiry.pleaseEnter')" clearable style="width:100%"></el-input>
                        </el-form-item>
                    </el-col>
                </el-row>
            </condition-card>

            <el-card shadow="never" v-loading="loading" body-style="padding: 24px;" class="card-margin">
                <h1 class="condition-title">{{$t('examinInquiry.examinationQueryList')}}</h1>
                <el-row style="padding:10px;" class="row-bg" justify="space-between">
                    <div>
                        <el-input
                            v-model.trim="queryForm.condition.student"
                            :placeholder="$t('examinInquiry.enterNumberOrSearch')"
                            style="width:250px"
                            @keyup.enter.native="onSearch"
                        >
                            <i
                                slot="suffix"
                                class="el-icon-search"
                                style="margin-top:10px;cursor:pointer"
                                @click="onSearch"
                            ></i>
                        </el-input>
                    </div>
                    <div>
                        <el-button type="primary" v-if="exportBut" style="float:right; margin-left:10px;" @click="export1">{{$t('examinInquiry.export')}}</el-button>
                        <el-button type="primary" v-if="statistBut" style="float:right; margin-left:10px;" @click="examinationStatistics">{{$t('examinInquiry.examinationStatistics')}}</el-button>
                    </div>
                </el-row>
                <edu-table :pagination="false" :selection='true' :order="false" :tooltipEffect="tooltipEffect" stripe :data="dataList" size="mini" @selection-change="handleSelectionChange">
                    <edu-table-column :label="$t('examinInquiry.studentNumber')" prop="studentId"></edu-table-column> <!-- 学号 -->
                    <edu-table-column :label="$t('examinInquiry.fullName')" prop="studentName"></edu-table-column> <!-- 姓名 -->
                    <edu-table-column :label="$t('examinInquiry.grade')" prop="grade"></edu-table-column> <!-- 年级 -->
                    <edu-table-column :label="$t('examinInquiry.college')" prop="collegeName"></edu-table-column> <!-- 学院 -->
                    <edu-table-column :label="$t('examinInquiry.major')" prop="majorName"></edu-table-column> <!-- 专业 -->
                    <edu-table-column :label="$t('examinInquiry.nameExamination')" prop="examName"></edu-table-column> <!-- 考试名称 -->
                    <edu-table-column :label="$t('examinInquiry.cultivationLevel')" prop="trainingLevelName"></edu-table-column>  <!-- 培养层次 -->
                    <edu-table-column :label="$t('examinInquiry.trainingCategory')" prop="trainingCategoryName"></edu-table-column> <!-- 培养类别 -->
                    <edu-table-column :label="$t('examinInquiry.degreeType')" prop="degreeTypeName"></edu-table-column> <!-- 学位类型 -->
                    <edu-table-column :label="$t('examinInquiry.learningForm')" prop="formLearningName"></edu-table-column> <!-- 学习形式 -->
                    <edu-table-column :label="$t('examinInquiry.attendanceTimes')" prop="count"></edu-table-column> <!-- 参加次数 -->
                    <edu-table-column :label="$t('examinInquiry.classNumber')" prop="courseCode"></edu-table-column> <!-- 课号 -->
                    <edu-table-column :label="$t('examinInquiry.tutor')" prop="teacherName"></edu-table-column> <!-- 导师 -->
                    <!-- 成绩 -->
                    <edu-table-column :label="$t('examinInquiry.achievement')" prop="score" min-width="120" align="center">
                        <template slot-scope="scope">
                            <el-tag
                                v-if="scope.row.score === '通过'"
                                type="success"
                                plain>
                                {{ scope.row.score}}
                            </el-tag>
                            <el-tag
                                v-else-if="scope.row.score === '未通过'"
                                type="danger"
                                plain>
                                {{ scope.row.score }}
                            </el-tag>
                        </template>
                    </edu-table-column>
                    <edu-table-column v-if="operBut" :label="$t('examinInquiry.operation')">
                        <template slot-scope="scope">
                            <el-button type="text" @click="check(scope.row)">{{ $t('examinInquiry.check') }}</el-button>
                        </template>
                    </edu-table-column>
                </edu-table>
                <el-row type="flex" class="row-bg" justify="end">
                    <el-col>
                        <el-pagination
                            @size-change="handleSizeChange"
                            @current-change="handleCurrentChange"
                            :current-page="queryForm.pageNum_"
                            :page-sizes="[10, 20, 30, 40]"
                            :page-size="queryForm.pageSize_"
                            layout="total, sizes, prev, pager, next, jumper"
                            :total="queryForm.total_"
                            style="float:right;margin-top:10px"></el-pagination>
                    </el-col>
                </el-row>
            </el-card>
            <edu-export-dialog
                ref="downloadRef"
                :disabled="exportButton"
                :title="$t('message.prompt')"
                :data="exportResult"
            >
            </edu-export-dialog>
        </div>
        <!-- 考试查询(考试统计部分) -->
        <div v-if="indexKey === 3">
            <examinatStatist
                :compile="compile"
                :data-list="dataListObj"
                @change-exam="changeExam">
            </examinatStatist>
        </div>
        <!-- 我的考试(查看部分) -->
        <div v-if="indexKey === 2">
            <examinationCheck
                :compile="compile"
                :dataList="dataObjList"
                :dataObj="dataObj"
                @change-exam="changeExam">
            </examinationCheck>
        </div>
    </div>
</template>

<script>
import { queryDic } from 'common/src/api/dictionary'
import UTILS from 'common/src/utils/utils'
import myExaminationsApi from 'common/src/api/academicBehaviorNorms/myExaminations/myExaminations';
import graduateStudent from 'common/src/api/graduateStudent/graduateStudent';
// import examCheck from './examinationCheck'
import examStatist from './examinaStatis'
import examCheck from 'common/src/views/academicBehaviorNorms/myExaminations/examinCheck'
import mixin from 'common/src/mixins/index';
export default {
    mixins: [mixin],
    components: {
        // examinationChecks: examCheck,
        examinatStatist: examStatist,
        examinationCheck: examCheck
    },
    data() {
        return {
            queryForm: {
                pageNum_: 1,
                pageSize_: 10,
                total_: 0,
                condition: {
                    grade: '', // 年级
                    college: '', // 学院
                    major: '', // 专业
                    trainingLevel: '', // 培养层次
                    trainingCategory: '', // 培养类别
                    degreeType: '', // 学位类型
                    formLearning: '', // 学习形式
                    teacher: '', // 导师 
                    score: '', // 成绩
                    courseCode: '', // 课号
                    count: '', // 参加次数
                    student: '',
                    studentId: '',
                    studentName: ''
                }
            },
            options: {
                college: [], // 学院
                major: [], // 专业
                grade: [], // 年级
                trainingLevel: [], // 培养层次
                trainingCategory: [], // 培养类别
                degreeType: [], // 学位类型
                formLearning: [], // 学习形式
                // 成绩
                score: [
                    { value: 1, label: '通过' },
                    { value: 2, label: '未通过' },
                ] 
            },
            dataList: [],
            indexKey: 1,
            compile: '',
            dataListObj: {},
            exportButton: false,
            exportResult: null,
            multipleSelection: [],
            records: [],
            condition: {},
            dataObj: {},
            dataObjList: [],
            exportBut: false,
            statistBut: false,
            operBut: false,
            auths: []
        }
    },
    watch: {
        'auths': {
            handler: function(value) {
                value.some(item => {
                    if (item.authCode === 'examInquiry-exportBut') {
                        // 导出
                        this.exportBut = true;
                    }
                    if (item.authCode === 'examInquiry-statistBut') {
                        // 考试统计
                        this.statistBut = true;
                    }
                    if (item.authCode === 'examInquiry-operBut') {
                        // 操作
                        this.operBut = true;
                    }
                });
            },
            deep: true
        }
    },
    created() {
        this.auths = JSON.parse(localStorage.getItem("sessiondata")).auths;
        this.auths.some(item => {
            if (item.authCode === 'examInquiry-exportBut') {
                // 导出
                this.exportBut = true;
            }
            if (item.authCode === 'examInquiry-statistBut') {
                // 考试统计
                this.statistBut = true;
            }
            if (item.authCode === 'examInquiry-operBut') {
                // 操作
                this.operBut = true;
            }
        });
        this.queryForm.condition.grade = this.$moment(new Date().getTime()).format('YYYY')
    },
    mounted() {
        this.getOptions()
        this.queryForm.condition.count = ''
        this.queryForm.condition.major = ''
        this.options.major = []
        this.getExaminationApi(this.queryForm)
    },
    filters: {
        score (val) {
            switch (val) {
                case 1:
                    return '通过'
                case 2:
                    return '未通过'
                default:
                    return ''
            }
        },
        getType (val) {
            switch (val) {
                case 1:
                    return 'success'
                case 2:
                    return 'danger'
                default: 
                    return ''
            }
        }
    },
    methods: {
        // 查看
        check(data) {
            let obj = {
                studentId: data.studentId,   // this.global.user.uid
                examId: data.examId
            }
            myExaminationsApi.getStuExamList(obj).then(res => {
                let list = res.data
                let lists = list.pop()
                let objs = {
                    studentId: data.studentId,
                    examId: data.examId,
                    id: lists.id
                }
                myExaminationsApi.queryExamRecord(objs).then(resd => {
                    this.dataObjList = resd.data
                    this.dataObj = objs
                    this.compile = '查看'
                    this.indexKey = 2 
                })
            })
        },
        // 查询接口
        getExaminationApi(data) {
            let parm = _.cloneDeep(this.queryForm)
            if(!parm.condition.count) {
                 parm.condition.count = ""
             }
            myExaminationsApi.getExamQueryPage(parm).then(res => {
                console.log(this.queryForm.condition.count)
                this.dataList = res.data.list
                this.queryForm.total_ = res.data.total_
                this.multipleSelection = []
            })
        },
         
        // 重置
        resetSearch() {
            for (let k in this.queryForm.condition) {
                this.queryForm.condition[k] = ''
            }
            this.options.major = []
            this.queryForm.pageNum_ = 1
            this.queryForm.condition.grade = this.$moment(new Date().getTime()).format('YYYY')
            this.getExaminationApi(this.queryForm)
        },
        // 筛选
        onSearch() {
            if (this.queryForm.condition.count === 0) {
                this.queryForm.condition.count = ''
            }
            this.queryForm.pageNum_ = 1
            this.getExaminationApi(this.queryForm)
        },
        // 导出
        export1() {
            let _this = this
            let parm = {
                condition: this.queryForm.condition,
                records: []
            }
            if (_this.dataList.length === 0) {
                _this.$message.error('无数据')
                return
            }
            if (_this.multipleSelection.length === 0) {
                parm = {
                    condition: this.queryForm.condition,
                    records: []
                }
            } else {
                _this.multipleSelection.forEach(el => {
                    parm.records.push(el)
                })
            }
            // this.queryForm.condition.count = ''
            myExaminationsApi.exports(parm).then(res => {
                _this.$message({
                    message: _this.$t('base.exportSuccess'),
                    type: 'success'
                })
            })
        },
        // 考试统计
        examinationStatistics() {
            this.compile = '考试统计'
            this.indexKey = 3
        },
        // 勾选
        handleSelectionChange(val) {
            this.multipleSelection = val
        },
        // 分页
        handleSizeChange(val) {
            this.queryForm.pageSize_ = val
            this.queryForm.pageNum_ = 1
            // this.queryForm.condition.count = ''
            this.getExaminationApi(this.queryForm)
            // this.queryForm.condition.count = ''
        },
        // 分页
        handleCurrentChange(val) {
            this.queryForm.pageNum_ = val
            // this.queryForm.condition.count = ''
            this.getExaminationApi(this.queryForm)
            // this.queryForm.condition.count = ''
        },
        // 返回列表主页
        changeExam(val) {
            this.indexKey = 1
            if(this.queryForm.condition.count == 0) {
                this.queryForm.condition.count = ''
            }
            if(val !== 'view') {
                this.multipleSelection = []
                this.queryForm.pageNum_ = 1
                for (let k in this.queryForm.condition) {
                    this.queryForm.condition[k] = ''
                }
                this.options.major = []
                this.queryForm.condition.grade = this.$moment(new Date().getTime()).format('YYYY')
            }
            
            this.getExaminationApi(this.queryForm)
        },
        // 筛选条件字典查询
        getOptions() {
            let data = {
                lang: this.$store.getters.language || "cn",
                type: "allChild",
                keys: [ 'X_PYCC', 'X_PYLB', 'X_XWLX', "X_XXXS", 'X_YX' ]
            };
            queryDic(data).then(res => {
                UTILS.fillSelect(
                    {
                        trainingLevel: "X_PYCC", // 培养层次
                        trainingCategory: "X_PYLB", // 培养类别
                        degreeType: "X_XWLX", // 学位类型
                        formLearning: "X_XXXS", // 学习形式
                        college: "X_YX" // 学院
                    },
                    res.data,
                    this.options
                );
            });
        },
        // 学院选择
        // collegeChange(val) {
        //     if (val.length > 0) {
        //         let collegeList = [val];
        //         graduateStudent.getMajorByCollage(collegeList).then(res => {
        //             this.options.major = res.data;
        //         });
        //     } else {
        //         this.options.major = [];
        //     }
        // },
        // 修改学院
        collegeChange(val) {
            this.options.major = []
            this.queryForm.condition.major = ''
            let data = {
                lang: this.$store.getters.language || 'cn',
                type: 'allChild',
                keys: ['G_ZY'],
                filter: {
                    prefix: [val],
                    specilaCode: 'XY2ZY',
                    facultys: [val]
                }
            }
            queryDic(data).then(res => {
                UTILS.fillSelect(
                    {
                        major: 'G_ZY' // 专业
                    },
                    res.data,
                    this.options
                )
            })
        }
    }
}
</script>


<style scoped>

</style>
